﻿@{
    ViewBag.Title = "Index";
}

<h2>Log in/Sign up</h2>

@Html.Partial("LogInSignUp")



@* SCRIPTS *@
<script>

    function customAlert(msg, duration) {
        var styler = document.createElement("div");
        styler.setAttribute("style", "border: solid 2px black;width:200px;height:100px;background-color:#F8F8FF;color:Grey ; position: absolute; top:80%; left:40%; bottom: 20%; right: 50%; border-radius:25px");
        styler.innerHTML = "<h2>" + msg + "<h2>";
        setTimeout(function () {
            styler.parentNode.removeChild(styler);
        }, duration);
        document.body.appendChild(styler);
    }



    // Sign up
    function SignUp() {
        var emailSplit = new Array();
        var agreeTerms = document.getElementById("Agree").checked;

        if (agreeTerms == true) {
            newPassword = document.getElementById("NewPassword").value;
            newPasswordRepeat = document.getElementById("NewPasswordRepeat").value;

            if (newPassword != newPasswordRepeat)
                alert("Error. Passwords do not match!");
            else {
                if (!document.getElementById("NewUserName").checkValidity()) {
                    alert("Please type a valid email address.");
                }
                else {
                    newName = document.getElementById("NewName").value;
                    newGender = document.getElementById("NewGender").value;
                    newDOB = document.getElementById("NewDOB").value;
                    newEmail = document.getElementById("NewUserName").value;

                    //check if required fields are not filled up
                    if (newName == "" || newGender == "" || newDOB == "" || newEmail == "" || newPassword == "") {
                        alert("Please fill in all the fields");
                    }
                    else {
                        SignUpPost();
                    }
                }
            }
        }
        else {
            alert("Please agree with the terms and conditions");
        }
    }
    function SignUpPost() {
        XMLhttpSignUp = new XMLHttpRequest();

        XMLhttpSignUp.open("POST", "/User/SignUpAjax", true);
        XMLhttpSignUp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLhttpSignUp.onreadystatechange = SignUpReqReady;
        //name, gender, address, ic, country, password, DOB, email
        sendStrSignUp = "name=" + newName + "&gender=" + newGender + "&password=" + newPassword + "&DOB=" + newDOB + "&email=" + newEmail;
        XMLhttpSignUp.send(sendStrSignUp);
    }
    function SignUpReqReady() {
        if (XMLhttpSignUp.readyState == 4 && XMLhttpSignUp.status == 200) {
            //status is inside
            alert(XMLhttpSignUp.responseText);
            if (XMLhttpSignUp.responseText == "Successfully sign up!") {
                document.getElementById("homePage").click();
            }

        } else if (XMLhttpSignUp.readyState == 4) {
            alert("error! code: " + XMLhttpSignUp.status);
        }
    }



    // Log in
    function LogIn() {
        username = document.getElementById("Username").value;
        password = document.getElementById("Password").value;

        LogInPost();
    }  
    function LogInPost() {
        XMLhttpLogIn = new XMLHttpRequest();

        XMLhttpLogIn.open("POST", "/User/LoginAjax", true);
        XMLhttpLogIn.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLhttpLogIn.onreadystatechange = LogInReqReady;
        sendStrLogIn = "email=" + username + "&password=" + password;
        XMLhttpLogIn.send(sendStrLogIn);
    }
    function LogInReqReady() {
        if (XMLhttpLogIn.readyState == 4 && XMLhttpLogIn.status == 200) {

            if (XMLhttpLogIn.responseText == "Successfully log in!") {
                customAlert("Successfully logged in", "2000");
                document.getElementById("homePage").click();
            }
            else {
                alert(XMLhttpLogIn.responseText);
            }

        } else if (XMLhttpLogIn.readyState == 4) {
            alert("error! code: " + XMLhttpLogIn.status);
        }
    }



</script>

<form action="/Home/Index" method="post">
    <input type="submit" name="submit" id="homePage" hidden="hidden"/>
</form>





 

@*
    function reqReadyResetPassword() {
        if (XMLhttpResetPassword.readyState == 4 && XMLhttpResetPassword.status == 200) {
            if (XMLhttpResetPassword.response == "True") {
                alert("New password has been sent to your email");
            }
            else {
                alert("Invalid email.");
            }
            

        } else if (XMLhttpResetPassword.readyState == 4) {
            alert("error! code: " + XMLhttpResetPassword.status);

        }
    }
    function ResetPassword() {
        email = document.getElementById("Username").value;

        if (email == "") {
            alert("Please key in your email");
        }
        else {
            XMLhttpResetPassword = new XMLHttpRequest();
            XMLhttpResetPassword.open("POST", "/Customer/ResetPassword", true);
            XMLhttpResetPassword.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            XMLhttpResetPassword.onreadystatechange = reqReadyResetPassword;
            sendStrResetPassword = "email=" + email;
            XMLhttpResetPassword.send(sendStrResetPassword);
        }

    } *@